package com.zl.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Controller;

/**
 * @Author ljm
 * @Date 2021/11/25 15:09
 */
@Controller
//@Aspect//声明该类是切面对象
public class MethodDateAspect {
    private long beforeDateTime;
    //@Before("execution(* com.zl.service.EmpService.addEmp(..))")
    //@Before("execution(* com.zl.service..*.*(..))")
    //@Before("execution(* com.zl.service..*.add*())")
    //@Before("execution(* com.zl.service..*.add*()) || execution(* com.zl.service..*.update*())")
    //@Before("execution(* com.zl.service..*.query*()) && !execution(* com.zl.service..*.*Emp())")
    //@Before("execution(* com.zl.controller..*.*(..))")
    public void before(JoinPoint jp){
        //beforeDateTime = new Date().getTime();
        beforeDateTime = System.currentTimeMillis();
    }
    //@After("execution(* com.zl.controller..*.*(..))")
    public void after(JoinPoint jp){
        //System.out.println(jp.getSignature().getName()+"方法执行完花费了:"+(new Date().getTime()-beforeDateTime)+"毫秒");
        System.out.println(jp.getSignature().getName()+"方法执行完花费了:"+(System.currentTimeMillis()-beforeDateTime)+"毫秒");
    }
    //@AfterReturning(pointcut = "execution(* com.zl.controller..*.*(..))",returning = "i")
    public void afterReturn(JoinPoint jp,Object i){
        System.out.println(jp.getSignature()+"的返回值为:" + i);
    }
    //@AfterThrowing(pointcut = "execution(* com.zl.controller..*.*(..))",throwing = "e")
    public void error(Exception e){
        System.out.println("这是异常通知:" + e);
    }
    //@Around("execution(* com.zl.controller..*.*(..))")
    //@ResponseBody
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知方法前");
        Object obj = null;
        obj = joinPoint.proceed();// 要不要放行
        //obj = "1234214";// 要不要修改返回值
        System.out.println("环绕通知方法后");
        return obj;
    }
}
